package com.example.aidl_threeloign;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;

import com.example.login.IListener;
import com.example.login.IPuduAidlInterface;

public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";
    private IPuduAidlInterface puduAidlInterface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //bing service
        Intent intent = new Intent();
        intent.setAction("login_action");
        intent.setPackage("com.example.aidl_loginresult");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        bindService(intent, connt, BIND_AUTO_CREATE);
    }

    /**
     * 第三方登录
     *
     * @param view
     */
    public void login(View view) {
        if (isConnect) {
            try {
                puduAidlInterface.login();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
//        Intent intent = new Intent(Intent.ACTION_VIEW);
//        String packageName = "com.example.aidl_loginresult";
//        String className = "com.example.aidl_loginresult.MainActivity";
//        intent.setClassName(packageName, className);
//        startActivity(intent);


    }

    private boolean isConnect;
    private ServiceConnection connt = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Log.e(TAG, "onServiceConnected: ----");
            isConnect = true;
            puduAidlInterface = IPuduAidlInterface.Stub.asInterface(iBinder);
            try {
                puduAidlInterface.registListener(loginListener);


            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Log.e(TAG, "onServiceDisconnected: ----");
        }
    };

    public IListener loginListener = new IListener.Stub() {
        @Override
        public void loginResult(String msg) throws RemoteException {
            Log.e(TAG, "login result: " + msg);
        }
    };
}
